package com.alibaba.dingtalk.openapi.newmessage.helper;/*/*/

import com.alibaba.dingtalk.openapi.exception.ApiRuleException;
import com.alibaba.dingtalk.openapi.exception.OApiException;
import com.alibaba.dingtalk.openapi.exception.OApiResultException;
import com.alibaba.dingtalk.openapi.helper.Env;
import com.alibaba.dingtalk.openapi.newmessage.delivery.CorpConversationProgressMsgDelivery;
import com.alibaba.dingtalk.openapi.newmessage.delivery.CorpConversationSendByTemplateDelivery;
import com.alibaba.dingtalk.openapi.newmessage.delivery.CorpConversationSendMsgDelivery;
import com.alibaba.dingtalk.openapi.newmessage.delivery.CorpConversationSendResultMsgDelivery;
import com.alibaba.dingtalk.openapi.newmessage.utils.HttpHelper;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Objects;

/**
 * 工作通知消息
 * @author 魏荣杰
 * @date 2019/3/21 15:02
 * @since v1.0
 * @version v1.0
 */
public class CorpConversationHelper {
    private static final Logger LOG = LoggerFactory.getLogger(CorpConversationHelper.class);

    @Setter
    @Getter
    @ToString
    public static class BaseResponse {
        Long errcode;
        String errmsg;
    }

    @Setter
    @Getter
    @ToString
    public static class SendResponse extends BaseResponse {
        Long taskId;
    }

    @Setter
    @Getter
    @ToString
    public static class ProgressResponse extends BaseResponse {

        private Progress progress;

        @Getter
        @Setter
        @ToString
        public static class Progress {
            /**
             * 取值 0-100，表示处理的百分比
             */
            private Integer progressInPercent;
            /**
             * 任务执行状态，0=未开始，1=处理中，2=处理完毕
             */
            private Integer status;
        }
    }

    @Setter
    @Getter
    @ToString
    public static class SendResultResponse extends BaseResponse {

        private SendResult sendResult;

        @Setter
        @Getter
        @ToString
        public static class SendResult {
            /**
             * 无效的用户id
             */
            private String invalidUserIdList;
            /**
             * 因发送消息超过上限而被流控过滤后实际未发送的userid。未被限流的接收者仍会被收到消息。
             * 限流规则包括：
             * 1、同一个微应用相同消息的内容同一个用户一天只能接收一次。 2、同一个微应用给同一个用户发送消息，如果是ISV应用开发方式一天不得超过50次；如果是企业内部开发方式，此上限为500次。
             */
            private String forbiddenUserIdList;
            /**
             * 发送失败的用户id
             */
            private String failedUserIdList;
            /**
             * 已读消息的用户id
             */
            private String readUserIdList;
            /**
             * 未读消息的用户id
             */
            private String unreadUserIdList;
            /**
             * 无效的部门id
             */
            private String invalidDeptIdList;
        }
    }

    @Setter
    @Getter
    @ToString
    public static class SendByTemplateResponse extends BaseResponse {
        /**
         * 创建的异步发送任务ID
         */
        Long taskId;

        /**
         * 请求ID
         */
        String requestId;
    }

    /**
     * Description: 发送工作通知消息
     * @param accessToken
     * @param corpConversationSendMsgDelivery 
     * @return com.alibaba.dingtalk.openapi.newmessage.helper.CorpConversationHelper.SendResponse
     * @throws OApiException OApiException
     * @author 魏荣杰
     * @date 2019/3/22 15:07
     * @since v1.0
     */
    public static SendResponse send(String accessToken, CorpConversationSendMsgDelivery corpConversationSendMsgDelivery) throws OApiException {
        String url = Env.OAPI_HOST + "/topapi/message/corpconversation/asyncsend_v2?access_token=" + accessToken;
        JSONObject response;
        try {
            corpConversationSendMsgDelivery.check();
            response = HttpHelper.httpPost(url, corpConversationSendMsgDelivery.toJsonObject());
        } catch (ApiRuleException e) {
            LOG.error("Request /topapi/message/corpconversation/asyncsend_v2 param error", e);
            throw new OApiResultException("Request /topapi/message/corpconversation/asyncsend_v2 param check error!");
        } catch (OApiException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("Access /topapi/message/corpconversation/asyncsend_v2 error", e);
            throw new OApiResultException("Access /topapi/message/corpconversation/asyncsend_v2 error!");
        }

        SendResponse sendResponse = null;
        if (!Objects.isNull(response)) {
            sendResponse = JSONObject.parseObject(response.toJSONString(), SendResponse.class);
        }

        return sendResponse;
    }

    /**
     * Description: 查询工作通知消息的发送进度
     * @param accessToken
     * @param corpConversationProgressMsgDelivery
     * @return com.alibaba.dingtalk.openapi.newmessage.helper.CorpConversationHelper.ProgressResponse
     * @throws OApiException OApiException
     * @author 魏荣杰
     * @date 2019/3/22 15:09
     * @since v1.0
     */
    public static ProgressResponse progress(String accessToken, CorpConversationProgressMsgDelivery corpConversationProgressMsgDelivery) throws OApiException {
        String url = Env.OAPI_HOST + "/topapi/message/corpconversation/getsendprogress?access_token=" + accessToken;
        JSONObject response;
        try {
            corpConversationProgressMsgDelivery.check();
            response = HttpHelper.httpPost(url, corpConversationProgressMsgDelivery.toJsonObject());
        } catch (ApiRuleException e) {
            LOG.error("Request /topapi/message/corpconversation/getsendprogress param error", e);
            throw new OApiResultException("Request /topapi/message/corpconversation/getsendprogress param check error!");
        } catch (OApiException e) {
            throw e;
        }  catch (Exception e) {
            LOG.error("Access /topapi/message/corpconversation/getsendprogress error", e);
            throw new OApiResultException("Access /topapi/message/corpconversation/getsendprogress error!");
        }

        ProgressResponse progressResponse = null;
        if (!Objects.isNull(response)) {
            progressResponse = JSONObject.parseObject(response.toJSONString(), ProgressResponse.class);
        }
        return progressResponse;
    }

    /**
     * Description: 查询工作通知消息的发送结果
     * @param accessToken
     * @param corpConversationSendResultMsgDelivery
     * @return com.alibaba.dingtalk.openapi.newmessage.helper.CorpConversationHelper.SendResultResponse
     * @throws OApiException OApiException
     * @author 魏荣杰
     * @date 2019/3/22 15:10
     * @since v1.0
     */
    public static SendResultResponse sendResult(String accessToken, CorpConversationSendResultMsgDelivery corpConversationSendResultMsgDelivery) throws OApiException {
        String url = Env.OAPI_HOST + "/topapi/message/corpconversation/getsendresult?access_token=" + accessToken;
        JSONObject response;
        try {
            corpConversationSendResultMsgDelivery.check();
            response = HttpHelper.httpPost(url, corpConversationSendResultMsgDelivery.toJsonObject());
        } catch (ApiRuleException e) {
            LOG.error("Request /topapi/message/corpconversation/getsendresult param error", e);
            throw new OApiResultException("Request /topapi/message/corpconversation/getsendresult param check error!");
        } catch (OApiException e) {
            throw e;
        }  catch (Exception e) {
            LOG.error("Access /topapi/message/corpconversation/getsendresult error", e);
            throw new OApiResultException("Access /topapi/message/corpconversation/getsendresult error!");
        }

        SendResultResponse sendResultResponse = null;
        if (!Objects.isNull(response)) {
            sendResultResponse = JSONObject.parseObject(response.toJSONString(), SendResultResponse.class);
        }
        return sendResultResponse;
    }

    
    /**
     * Description: 使用模板发送工作通知
     * @param accessToken
	 * @param corpConversationSendByTemplateDelivery
     * @return com.alibaba.dingtalk.openapi.newmessage.helper.CorpConversationHelper.SendByTemplateResponse
     * @throws 
     * @author 魏荣杰
     * @date 2021/12/2 14:19
     * @since
     */
    public static SendByTemplateResponse sendByTemplate(String accessToken, CorpConversationSendByTemplateDelivery corpConversationSendByTemplateDelivery) throws OApiException {
        String url = Env.OAPI_HOST + "/topapi/message/corpconversation/sendbytemplate?access_token=" + accessToken;
        JSONObject response;
        try {
            corpConversationSendByTemplateDelivery.check();
            response = HttpHelper.httpPost(url, corpConversationSendByTemplateDelivery.toJsonObject());
        } catch (ApiRuleException e) {
            LOG.error("Access /topapi/message/corpconversation/sendbytemplate error", e);
            throw new OApiResultException("Request /topapi/message/corpconversation/sendbytemplate param check error!");
        } catch (OApiException e) {
            throw e;
        } catch (Exception e) {
            LOG.error("Access /topapi/message/corpconversation/sendbytemplate error", e);
            throw new OApiResultException("Request /topapi/message/corpconversation/sendbytemplate error!");
        }

        SendByTemplateResponse sendByTemplateResponse = null;
        if (!Objects.isNull(response)) {
            sendByTemplateResponse = JSONObject.parseObject(response.toJSONString(), SendByTemplateResponse.class);
        }
        return sendByTemplateResponse;
    }
}
